<html>
<head><meta charset="utf-8"><title>Getting Box&lt;str&gt; from either String or str? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html">Getting Box&lt;str&gt; from either String or str?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="194979511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194979511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194979511">(Apr 22 2020 at 19:26)</a>:</h4>
<p>What combination of existing traits would allow me to get a <code>Box&lt;str&gt;</code> out of either <code>String</code> or <code>&amp;str</code>, without having to create a new custom trait, and without making a copy if given a <code>String</code>? I'd like to call <code>.into_boxed_str()</code> if given a <code>String</code> (which doesn't copy), or copy the <code>str</code> into a box if given a <code>&amp;str</code>.</p>



<a name="194979659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194979659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194979659">(Apr 22 2020 at 19:27)</a>:</h4>
<p>hm, do you want.. traits?</p>



<a name="194979667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194979667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194979667">(Apr 22 2020 at 19:27)</a>:</h4>
<p>or would a method be sufficient</p>



<a name="194979749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194979749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194979749">(Apr 22 2020 at 19:28)</a>:</h4>
<p>oh, I see, you're intending to accept via a function argument</p>



<a name="194979841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194979841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194979841">(Apr 22 2020 at 19:28)</a>:</h4>
<p><code>Box&lt;str&gt;</code> already implements both <code>From&lt;String&gt;</code> and <code>From&lt;&amp;str&gt;</code></p>



<a name="194980661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194980661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194980661">(Apr 22 2020 at 19:34)</a>:</h4>
<p><span class="user-mention" data-user-id="138448">@cuviper</span> Oh, hey, that's perfect.</p>



<a name="194980749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194980749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194980749">(Apr 22 2020 at 19:35)</a>:</h4>
<p>Somehow it never seems to occur to me to look for <code>From</code> impls, especially when I see explicit methods like <code>String::into_boxed_str</code>.</p>



<a name="194980753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194980753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194980753">(Apr 22 2020 at 19:35)</a>:</h4>
<p>Thank you!</p>



<a name="194980767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194980767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194980767">(Apr 22 2020 at 19:35)</a>:</h4>
<p>I think they're relatively recent fwiw</p>



<a name="194980972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194980972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194980972">(Apr 22 2020 at 19:37)</a>:</h4>
<p>Sure enough, accepting <code>Into&lt;Box&lt;str&gt;&gt;</code> gets me exactly what I want.</p>



<a name="194981831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194981831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194981831">(Apr 22 2020 at 19:43)</a>:</h4>
<p>On a somewhat related note: would it make sense to have <code>From&lt;String&gt;</code> and <code>From&lt;&amp;str&gt;</code> (and <code>From&lt;Box&lt;str&gt;&gt;</code>) for <code>Box&lt;[u8]&gt;</code>?</p>



<a name="194983840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194983840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194983840">(Apr 22 2020 at 19:59)</a>:</h4>
<p>indirectly, there is <code>impl From&lt;Box&lt;str&gt;&gt; for Box&lt;[u8]&gt;</code></p>



<a name="194983965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194983965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194983965">(Apr 22 2020 at 20:00)</a>:</h4>
<p>FWIW, I'm now working on <code>From&lt;Cow&gt; for Box</code> inspired from here</p>



<a name="194983974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194983974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194983974">(Apr 22 2020 at 20:00)</a>:</h4>
<p>personally I'm not a fan of the niche From impls, but ... not sure</p>



<a name="194984024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194984024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194984024">(Apr 22 2020 at 20:00)</a>:</h4>
<p><span class="user-mention" data-user-id="138448">@cuviper</span> Right, but that wouldn't allow <code>param: impl Box&lt;[u8]&gt;</code> to accept <code>String</code> or <code>&amp;str</code>, right?</p>



<a name="194984026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194984026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194984026">(Apr 22 2020 at 20:00)</a>:</h4>
<p>they always feel a bit odd to have, even if not <em>wrong</em></p>



<a name="194984093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Getting%20Box%3Cstr%3E%20from%20either%20String%20or%20str%3F/near/194984093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Getting.20Box.3Cstr.3E.20from.20either.20String.20or.20str.3F.html#194984093">(Apr 22 2020 at 20:01)</a>:</h4>
<p>correct, that <code>param</code> wouldn't work through two layers of <code>From</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>